﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharePointSlideShow.Model;
using Microsoft.SharePoint;
using SharePointSlideShow.Utility;

namespace SharePointSlideShow.Persistence
{
    public class SlideImage
    {
       
        public static SPListItemCollection GetImages(string Siteurl,string ListName)
        {
            SPListItemCollection ImagesCollection=null;
            try
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                using (SPWeb web = site.OpenWeb(Siteurl))
                {
                    SPList list = web.Lists[ListName];
                    SPQuery query = new SPQuery();
                    query.Query = GetSortOrder();
                    ImagesCollection = list.GetItems(query);
                    
                }

                
            }
            catch (Exception ex)
            {
                SlideShowLogging.LogErrorInULS("Error in getting the images"+ex.Message);
            }
            return ImagesCollection;
        }
        public static SPListItemCollection GetImages(string Siteurl, string ListName, int NoOfImages)
        {
            SPListItemCollection ImagesCollection = null;
            try
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                using (SPWeb web = site.OpenWeb(Siteurl))
                {
                    SPList list = web.Lists[ListName];
                    SPQuery query = new SPQuery();
                    query.Query = GetSortOrder();
                    query.RowLimit =(uint) NoOfImages;
                    ImagesCollection = list.GetItems(query);
                }


            }
            catch (Exception ex)
            {
                SlideShowLogging.LogErrorInULS("Error in getting the images" + ex.Message);
            }
            return ImagesCollection;
        }
        public static SPListItemCollection GetImages(string Siteurl, string ListName, string FolderName)
        {
            SPListItemCollection ImagesCollection = null;
            try
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                using (SPWeb web = site.OpenWeb(Siteurl))
                {
                    SPList list = web.Lists[ListName];
                    SPQuery query = new SPQuery();
                    query.Query = GetSortOrder();
                    SPFolder folder = list.RootFolder.SubFolders[FolderName];
                    query.Folder = folder;
                    ImagesCollection = list.GetItems(query);
                }


            }
            catch (Exception ex)
            {
                SlideShowLogging.LogErrorInULS("Error in getting the images" + ex.Message);

            }
            return ImagesCollection;
        }
        public static SPListItemCollection GetImages(string Siteurl, string ListName, bool IncludeFolder)
        {
            SPListItemCollection ImagesCollection =null;
            try
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                using (SPWeb web = site.OpenWeb(Siteurl))
                {
                    SPList list = web.Lists[ListName];
                    SPQuery query = new SPQuery();
                    query.Query = GetSortOrder();
                    query.ViewAttributes = "Scope='RecursiveAll'";
                    ImagesCollection = list.GetItems(query);
                }


            }
            catch (Exception ex)
            {
                SlideShowLogging.LogErrorInULS("Error in getting the images" + ex.Message);
            }
            return ImagesCollection;
        }
        public static SPListItemCollection GetImages(string Siteurl, string ListName, bool IncludeFolder, string FolderName)
        {
            SPListItemCollection ImagesCollection =null;
            try
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                using (SPWeb web = site.OpenWeb(Siteurl))
                {
                    SPList list = web.Lists[ListName];
                    SPQuery query = new SPQuery();
                    query.Query = GetSortOrder();
                    query.ViewAttributes = "Scope='RecursiveAll'";
                    SPFolder folder = list.RootFolder.SubFolders[FolderName];
                    query.Folder = folder;
                    ImagesCollection = list.GetItems(query);
                }


            }
            catch (Exception ex)
            {
                SlideShowLogging.LogErrorInULS("Error in getting the images" + ex.Message);
            }
            return ImagesCollection;
        }
        public static SPListItemCollection GetImages(string Siteurl, string ListName, int NoofImages, string FolderName)
        {
            SPListItemCollection ImagesCollection =null;
            try
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                using (SPWeb web = site.OpenWeb(Siteurl))
                {
                    SPList list = web.Lists[ListName];
                    SPQuery query = new SPQuery();
                    query.Query = GetSortOrder();
                    SPFolder folder = list.RootFolder.SubFolders[FolderName];
                    query.Folder = folder;
                    query.RowLimit = (uint)NoofImages;
                    ImagesCollection = list.GetItems(query);
                }


            }
            catch (Exception ex)
            {
                SlideShowLogging.LogErrorInULS("Error in getting the images" + ex.Message);
            }
            return ImagesCollection;
        }
        public static SPListItemCollection GetImages(string Siteurl, string ListName, int NoofImages, bool IncludeFolder)
        {
            SPListItemCollection ImagesCollection =null;
            try
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                using (SPWeb web = site.OpenWeb(Siteurl))
                {
                    SPList list = web.Lists[ListName];
                    SPQuery query = new SPQuery();
                    query.Query = GetSortOrder();
                    query.ViewAttributes = "Scope='RecursiveAll'";
                    query.RowLimit = (uint)NoofImages;
                    ImagesCollection = list.GetItems(query);
                }


            }
            catch (Exception ex)
            {
                SlideShowLogging.LogErrorInULS("Error in getting the images" + ex.Message);

            }
            return ImagesCollection;
        }
        public static SPListItemCollection GetImages(string Siteurl, string ListName, bool IncludeFolder, string FolderName, int NoofImages)
        {
            SPListItemCollection ImagesCollection =null;
            try
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                using (SPWeb web = site.OpenWeb(Siteurl))
                {
                    SPList list = web.Lists[ListName];
                    SPQuery query = new SPQuery();
                    query.ViewAttributes = "Scope='RecursiveAll'";
                   
                    SPFolder folder = list.RootFolder.SubFolders[FolderName];
                    query.Folder = folder;
                    query.RowLimit = (uint)NoofImages;
                    ImagesCollection = list.GetItems(query);
                }


            }
            catch (Exception ex)
            {
                SlideShowLogging.LogErrorInULS("Error in getting the images" + ex.Message);
            }
            return ImagesCollection;
        }


        private static string GetSortOrder()
        {
            return "<OrderBy><FieldRef Name=\"Modified\" Ascending=\"False\" /></OrderBy>";
        }
        
    }
}
